package com.example.readandwriteseparation.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.readandwriteseparation.anno.Db;
import com.example.readandwriteseparation.enitiy.Orders;
import com.example.readandwriteseparation.mapper.OrdersMapper;
import com.example.readandwriteseparation.service.IOrdersService;
import com.example.readandwriteseparation.util.SwitchDB;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单
 *
 * @author qingyan 2021-09-02
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Override
    public void insert(Orders orders) {
        baseMapper.insert(orders);
    }

    @Override
    @Db("slaver")
    public List<Orders> list() {
        return super.list(Wrappers.<Orders>query().orderByDesc("create_time").last("limit 5"));
    }

    @Override
    public List<Orders> listByMaster() {
        return super.list(Wrappers.<Orders>query().orderByDesc("create_time").last("limit 5"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(List<Orders> orders) {
        for (Orders order : orders) {
            insert(order);
        }
    }

    @Override
    public void switchDb() {
        Orders order = new Orders()
                .setSupplierId(1)
                .setSupplierOrderCode("1")
                .setOrderCode(UUID.fastUUID().toString())
                .setMenuId(1)
                .setCandidateId(1)
                .setSubmitDateTime(LocalDateTime.now())
                .setSubmitUserId(1)
                .setCostDays(10)
                .setAuthorizeType(0)
                .setTotalMoney(BigDecimal.ZERO)
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        insert(order);
        SwitchDB.change("slaver");
        listByMaster();
        SwitchDB.reset();
    }
}
